﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Data;
using WPFExcelReport;
using WPFExcelReport.Helpers;
namespace ReflectionStudio.Controls
{
    
    public class JWCONVERTER : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string v = value.ToString().Trim();
            if (v=="")
                return"JW";
            if (v == "R")
                return "W";
            if (v == "W"||v=="K")
                return "J";
            return "";
        }

        /// <summary>
        /// IValueConverter.ConvertBack
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string v = value.ToString().Trim();
            if (v == "")
                return "JW";
            if (v == "W")
                return "R";
            if (v == "J")
                return "W";
            return "";
        }
    }
    public class DXMCONVERTER : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string v = value.ToString().Trim();
            if (v == "大")
                return "D ";
            if (v == "小")
                return "X ";
            if (v == "迷你")
                return "M ";
            return "";
        }

        /// <summary>
        /// IValueConverter.ConvertBack
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string v = value.ToString().Trim();
            if (v == "D ")
                return "大";
            if (v == "X ")
                return "小";
            if (v == "M ")
                return "迷你";
            return "";
        }
    }
    /// <summary>
    /// A converter for trimming text.
    /// </summary>
    public class TextTrimmingConverter : IValueConverter
    {
        /// <summary>
        /// Returns the converted value.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converter value.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        //    string text = value.ToString();
            /*int trimLength = text.Length;

            if (parameter != null)
            {
                int.TryParse(parameter.ToString(), out trimLength);
            }

            string textEnd = string.Empty;
            if (trimLength < text.Length)
            {
                textEnd = "...";
            }
            else if (trimLength > text.Length)
            {
                trimLength = text.Length;
            }
            */
            GB2312Convert8859P1 gb2312 = new GB2312Convert8859P1();
            string text=(string)gb2312.Convert(value, targetType, parameter, culture);
            return text.Trim();
        }

        /// <summary>
        /// Converts a value back.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converted back value.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
    public class SubStrConverter : IValueConverter
    {
        /// <summary>
        /// Returns the converted value.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converter value.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string split = ((string)parameter);
            int idx = split.IndexOf('+');
            if (idx < 0)
                return value;
            string vu =Helper.Convert8859P1ToGB2312( value.ToString().Trim()); 
            int left = int.Parse(split.Substring(0, idx));
            int lg = int.Parse(split.Substring(idx + 1));
            vu = vu.PadLeft(left + lg);
            return vu.Substring(left, lg);
        }

        /// <summary>
        /// Converts a value back.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converted back value.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
    /// <summary>
    /// A converter for trimming text.
    /// </summary>
    public class UpperConverter : IValueConverter
    {
        /// <summary>
        /// Returns the converted value.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converter value.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return value.ToString().ToUpper();
        }

        /// <summary>
        /// Converts a value back.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The converted back value.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString().ToUpper();
        }
    }
}
